#include "gtest/gtest.h"
#include "MessageHandler.h"
#include <thread>

#include "IpcServer.h"
#include "SocketServerChannel.h"
#include "IpcChannelManager.h"
#include "IpcType.h"
#include "IpcService.h"
#include "IpcSimService.h"
#include "IpcServiceManager.h"

#define EVENT_TEST_MSG   1
static void testHandlerMessage(Message* msg) {
    switch(msg->what) {
        case EVENT_TEST_MSG: {
            ASSERT_TRUE(TRUE);
            Handler* handler = (Handler*)msg->userObj;
            quit(handler->mQueue);
            break;
        }
        default:
            ASSERT_TRUE(FALSE);
            break;
    }
}


TEST(IpcServerTest, channelTest) {
    ipcServerInit();
    IpcChannelManager* manager = getDefaultIpcChannelManager();
    IpcChannel* channel = newSocketServerChannel(0);
    addChannel(manager, channel);

    IpcServiceManager* serviceManager = getDefaultIpcServiceManager();
    addService(serviceManager, newIpcSimService());

    prepare(TRUE);
    Handler* handler = newHandler(testHandlerMessage);

    std::thread thread = std::thread([channel, handler, this]() {
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    });
    loop();
    ipcServerdeinit();
    deleteHandler(handler);
    thread.join();
}

